/**********************************************************************/
/*
   Author: Michelle Han
   Created: Jan 2023
   Updated: June 2024
   Description: Cleans raw qualtrics survey data from 2022 survey. To be run on
   qualtrics dataset exported as excel file with choice text, with seen but unanswered
   questions coded as -99.

   Note that this file cleans both control group survey respondents and full survey respondents.
   Note that the output for this file contains only completed, verified and consented responses

   This cleaning file should output 3 different datasets:
   1. Cleaned Survey 22 data:
   071323_survey_21_deid_clean.dta
   2. Survey 22 data merged with Admin data:
   071323_survey_21_deid_clean_merged.dta
   3. Survey 22 data merged with Admin data: and subset to control group:
   071323_survey_21_deid_clean_control_merged.dta
*/
/**********************************************************************/

/*----------------------------------------------------*/
 * Section: Set-up and import de-identified data
/*----------------------------------------------------*/

* include filepaths
    if "$master_run" !="1" include "./Do/SET_FILEPATHS.do"

	clear
	set more off

	* Log
	cap log close
	global prefix: display %tdCYND td(`c(current_date)')
	log using "$KP_logs/${prefix}_clean_survey_22.txt", text replace

	* Load data
	use "$KP_deid_survey/2022/Raw/050523_survey_22_deid.dta", clear

	* Set trim values
	gen inc_sak_u = 10000000 // 99% percentile in sak aug '21
	gen inc_sak_l = 200000 // 5% percentile in sak aug '21
	gen wage_sak_u = 65000
	gen wage_sak_l = 0
	gen hrs_sak_u = 168
	gen food_sus_u = 8039887
	gen food_sus_l = 574206.6
	gen nfood_sus_u = 12143393
	gen nfood_sus_l = 348552.8

/*----------------------------------------------------*/
 * Section: Initial format
/*----------------------------------------------------*/

*********************************
* Destring numbers

	local destring_list progress ///
						durationinseconds ///
						dem09_4 ///
						kk12 ///
						kk13 ///
						dk07_2 ///
						dk09_1_* ///
						dk14_1_* ///
						dk07_v2_2 ///
						dk09_v2_1_* ///
						dk14_v2_1_* ///
						dk24_2 ///
						tr15_1 ///
						gf19_2 ///
						gf20_7_text ///
						gf27_2 ///
						gf28_4_text ///
						kp03_2 ///
						mobile ///
						year_batch ///
						bought ///
						sold ///
						mortgage ///
						work_hour_1 ///
						work_hour_2 ///
						re_random ///
						gender ///
						year_dob_inputted_plus2 ///
						year_dob_inputted_minus2 ///
						year_dob ///
						kp_branding ///
						incentive ///
						num_courses ///
						incentive* ///
						wtp_random ///
						dk06_dk10 ///
						ps01_ps02_last

	foreach i of varlist `destring_list' {
		destring `i', replace
		replace `i' = .a if `i' == -99
	}



*********************************
* Format date variables

	foreach timevar in startdate enddate recordeddate {
		* convert to stata date time
		rename `timevar' rawtime
		gen `timevar' = clock(rawtime, "YMDhms")
		format `timevar' %tc

		* convert to Indonesia time
		gen `timevar'_wib = `timevar'
		replace `timevar'_wib = `timevar'_wib + (13 * 60 * 60 * 1000)
		format `timevar'_wib %tc

		* calculate hour of day
		gen `timevar'Hour = hh(`timevar') + mm(`timevar')/60 + ss(`timevar')/3600
		drop raw
		summ `timevar' `timevar'_wib `timevar'Hour, format

		* create day variable
		gen `timevar'Day = dofc(`timevar')
		format `timevar'Day %td
	}


*********************************
* Format month variables

	la def months 1 "Januari" 2 "Februari" 3 "Maret" 4 "April" ///
				5 "Mei" 6 "Juni" 7 "Juli" 8 "Agustus" ///
				9 "September" 10 "Oktober" 11 "November" 12 "Desember"

	local month_vars dk07_1 ///
					dk07_v2_1 ///
					dk24_1 ///
					gf19_1 ///
					gf27_1 ///
					kp03_1 ///
					month_batch

	foreach var in `month_vars' {
		replace `var' = "1" if `var' == "Januari"
		replace `var' = "2" if `var' == "Februari"
		replace `var' = "3" if `var' == "Maret"
		replace `var' = "4" if `var' == "April"
		replace `var' = "5" if `var' == "Mei"
		replace `var' = "6" if `var' == "Juni"
		replace `var' = "7" if `var' == "Juli"
		replace `var' = "8" if `var' == "Agustus"
		replace `var' = "8" if `var' == "Augustus"	//spelling mistake in survey :(
		replace `var' = "9" if `var' == "September"
		replace `var' = "10" if `var' == "Oktober"
		replace `var' = "11" if `var' == "November"
		replace `var' = "12" if `var' == "Desember"

		destring `var', replace
		la val `var' months
		tab `var'
		count if `var' == .a
	}


*********************************
* Format Yes/No variables

	la def yesno 0 "No" 1 "Yes", replace

	local yesno_list cs_kp3_2 ///
					dem04 ///
					dem06 ///
					dk01 ///
					dk02 ///
					dk12 ///
					dk12_v2 ///
					dk16 ///
					dk19 ///
					dk21 ///
					dk23 ///
					tr13 ///
					tr14 ///
					tr19_* ///
					tr20_* ///
					tr25_1 ///
					tr25_2 ///
					tr25_3 ///
					tr25_4 ///
					tr25_1_check ///
					tr25_2_check ///
					tr25_3_check ///
					tr25_4_check ///
					kp01 ///
					kp02

	foreach var of varlist `yesno_list' {
	  assert inlist(`var', "Ya", "Tidak", "-99", "")
	  replace `var' = "1" if `var' == "Ya"
	  replace `var' = "0" if `var' == "Tidak"
	  destring `var', replace
	  replace `var' = .a if `var' == -99
	  la val `var' yesno
	  tab `var'
	  count if `var' == .a
	}


*********************************
* Clean rupiah amounts

	local rupiah_list kk01* ///
					kk02* ///
					ka02* ///
					ka03* ///
					ka04* ///
					ka06* ///
					ka07* ///
					ka08* ///
					ka10* ///
					ka11* ///
					ka12* ///
					ka14* ///
					ka15* ///
					ka16* ///
					dk06 ///
					dk06_1 ///
					dk06_v2* ///
					dk15 ///
					dk15_1 ///
					dk15_v2* ///
					tr22_*

	foreach i of varlist `rupiah_list' {

		gen `i'_missing = `i' == "-99"

		* Manual replace for common string inputs
		replace `i' = "1000000" if `i' == "1 juta"

		* Clean of characters and periods
		gen `i'_clean = ustrregexra(`i',"[^0-9]","")

		* Replace as missing if response only contained characters
		replace `i'_missing = 1 if missing(`i'_clean) & !missing(`i')
		replace `i' = `i'_clean
		destring `i', replace

		* Record seen but missing responses as .a
		replace `i' = .a if `i'_missing == 1
		drop `i'_missing `i'_clean
	}

	* Replace with second input after prompt if second input is not missing
 	foreach i of varlist kk01 kk02 ka02 ka03 ka04 ka06 ka07 ka08 ka10 ///
		ka11 ka12 ka14 ka15 ka16 dk06 dk06_v2 dk15 dk15_v2 {
			replace `i' = `i'_1 if !missing(`i'_1)
			drop `i'_1
	}


*********************************
* Clean questions on a scale of 1-6
* If respondents reply with don't know, replace with .d
	local scale_1_6 dk10* ///
					dk20* ///
					re02* ///
					re03* ///
					tr24_* ///
					gf01 ///
					gf02 ///
					gf03 ///
					gf04 ///
					gf05 ///
					gf06 ///
					gf07 ///
					gf08

	foreach var of varlist `scale_1_6' {
		replace `var' = "1" if strpos(`var', "1") > 0
		replace `var' = "6" if strpos(`var', "6") > 0
		replace `var' = ".d" if strpos(`var', "Tidak tahu") > 0
		destring `var', replace
		replace `var' = .a if `var' == -99
	}


*********************************
* Clean questions on a scale of 0 - 24 months
* Note: should we replace 24+ months with something else?

	local months_1_24 dk08* ///
					  dk17 ///
					  dk22

	foreach var of varlist `months_1_24' {
		replace `var' = "0" if strpos(`var', "dari sebulan") > 0 // less than a month
		replace `var' = "25" if strpos(`var', "dari 24") > 0 // more than 24 months
		destring `var', replace
		replace `var' = .a if `var' == -99
	}


/*----------------------------------------------------*/
 * Section: Completion variables
/*----------------------------------------------------*/

* Generate indicator for respondent completing the survey
	tab finished, m
	gen completed = finished == "True"
	tab completed, m
	la var completed "Indicator for respondent completing the survey"
	la val completed yesno
	drop finished

* Generate indicator for respondent consenting to survey
	tab cs_kp3_2, m
	gen consented = cs_kp3_2 == 1 if !missing(cs_kp3_2)
	tab consented, m
	la var consented "Indicator for respondent consenting to the survey"
	la val consented yesno
	drop cs_kp3_2

* Generate indicator for respondent passing verification section before dropping time variables for now
	gen verified = !missing(time_4_firstclick)
	la var verified "Passed survey verification system"
	tab verified, m


/*----------------------------------------------------*/
 * Section: Drop unwanted responses
/*----------------------------------------------------*/

* Drop variables
	drop time_* v3* kk*_amount mobile treatment month_migration year_migration ///
		month_migration_number kab_migration kec_migration ///
		cohort kp_branding month_for_dk year_for_dk

	cap drop _merge

* Drop test observations
	drop if inlist(recipientlastname, "Test", "test")
	drop if missing(recipientlastname) | missing(recipientfirstname)

* Keep only verified, completed, consented responses
	// keep if completed == 1 & consented == 1 & verified == 1

* Confirm anon_id is ID
	gsort anon_id4 - verified - consented - completed
    quietly by anon_id4:  gen dup = cond(_N==1,0,_n)
	tab verified dup
	drop if dup>0 // note: dropping all dupes here, 30 in total

	isid anon_id4


/*----------------------------------------------------*/
* Section: Demographics*/
/*----------------------------------------------------*/

* dem02: gender
	* replace with second response if prompted
	replace ver04 = ver04_1 if !missing(ver04_1) & ver04_1 != "-99"
	drop ver04_1

	gen gender_survey = ver04 == "Laki-laki"
	replace gender_survey = 2 if ver04 == "Perempuan"
	replace gender_survey = 3 if ver04 == "Memilih untuk tidak menjawab"
	replace gender_survey = .a if ver04 == "-99"
	la def gender 1 "Male" 2 "Female" 3 "Prefer not to answer", replace
	la val gender_survey gender
	tab gender_survey, m
	la var gender_survey "Gender (survey)"
	drop ver04*

* dem03: marital status
	tab dem03,m
	replace dem03 = "1" if dem03 == "Belum kawin"
	replace dem03 = "2" if dem03 == "Kawin"
	replace dem03 = "3" if dem03 == "Cerai hidup"
	replace dem03 = "4" if dem03 == "Cerai mati"
	destring dem03, replace
	replace dem03 = .a if dem03 == -99
	la def marital_status 1 "Never married" 2 "Married" 3 "Divorced" 4 "Widowed", replace
	la val dem03 marital_status
	la var dem03 "Marital status (survey)"
	rename dem03 marital_status
	tab marital_status, m

* dem04: given birth past two years
	la var dem04 "Given birth in the past 2 years"
	rename dem04 given_birth
	tab given_birth, m
	
* dem06: Pregant
	la var dem06 "Currently pregnant"
	rename dem06 pregnant
	tab pregnant, m

* dem08: Childcare (multi-select question)
	tab dem08, m
	rename dem08 childcare

	gen childcare_notwork = strpos(childcare, "Saya/pasangan saya tidak bekerja, agar dapat merawat anak") > 0
	gen childcare_workhome = strpos(childcare, "Saya/pasangan saya bekerja dari rumah, agar dapat merawat anak") > 0
	gen childcare_family = strpos(childcare, "Anggota keluarga lainnya yang merawat anak") > 0
	gen childcare_nannyhome = strpos(childcare, "Membayar pengasuh / jasa mengasuh anak untuk merawat anak di rumah") > 0
	gen childcare_nannyout = strpos(childcare, "Membayar pengasuh / jasa mengasuh anak di luar rumah (contoh: Penitipan anak)") > 0
	gen childcare_NA = strpos(childcare, "Tidak memiliki anak dibawah 5 tahun") > 0
	gen childcare_other = strpos(childcare, "Lainnya") > 0

	la var childcare_notwork   "Respondent/spouse care for children less than 5 by not working"
	la var childcare_workhome  "Respondent/spouse care for children less than 5 by working at home"
	la var childcare_family    "Family member cares for children less than 5"
	la var childcare_nannyhome "Nanny cares for children less than 5 at home"
	la var childcare_nannyout  "Nanny cares for children less than 5 outside home"
	la var childcare_NA        "No children less than 5"
	la var childcare_other     "Other child care"
	la var childcare           "If you have kids less than 5 years old, what do you do for child care during the day?"

	foreach var of varlist childcare_* {
		replace `var' = . if childcare == ""
		replace `var' = .a if childcare == "-99"
		la val `var' yesno
	}

* dem09: How many people are in your household?
	rename dem09_4 dem09
	replace dem09 = .a if dem09 == -99
	la var dem09 "Household size"
	rename dem09 hh_size
	tab hh_size, m


/*----------------------------------------------------*/
                  /* Section: kk */
/*----------------------------------------------------*/

* kk01: food expenditure and kk02: non-food expenditure
	rename kk01 food_cons
	la var food_cons "Simple Consumption Qs: Food expenditure last month (rupiah)"

	gen food_cons_adj = food_cons
	replace food_cons_adj = food_sus_l if food_cons < food_sus_l & !mi(food_cons)
	replace food_cons_adj = food_sus_u if food_cons > food_sus_u & !mi(food_cons)
	gen food_cons_adj_l = log(food_cons_adj)
	sum food_cons_adj_l, d
	replace food_cons_adj = food_cons_adj/1000
	sum food_cons_adj, d

	rename kk02 nfood_cons
	la var nfood_cons "Simple Consumption Qs: Non-food expenditure last month (rupiah)"

	gen nfood_cons_adj = nfood_cons
	replace nfood_cons_adj = nfood_sus_l if nfood_cons < nfood_sus_l & !mi(nfood_cons)
	replace nfood_cons_adj = nfood_sus_u if nfood_cons > nfood_sus_u & !mi(nfood_cons)
	gen nfood_cons_adj_l = log(nfood_cons_adj)
	sum nfood_cons_adj_l, d
	replace nfood_cons_adj = nfood_cons_adj/1000
	sum nfood_cons_adj, d

	gen tot_cons_adj = food_cons_adj + nfood_cons_adj
	gen tot_cons_adj_l = log(tot_cons_adj)
	sum tot_cons_adj_l, d

	sum tot_cons_adj if completed == 1 & consented == 1 & verified == 1, d
	gen tot_cons_adj_amed = 1 if tot_cons_adj > r(p50) & !mi(tot_cons_adj)
	replace tot_cons_adj_amed = 0 if tot_cons_adj <= r(p50) & !mi(tot_cons_adj)

* kk12 and kk13: ladder questions
	rename kk12 ladder_now
	la var ladder_now "Ladder from best life to worst life: where do you personally feel you stand? (10-top, 1-bottom)"

	rename kk13 ladder_5years
	la var ladder_5years "Ladder from best life to worst life: where do you stand 5 years from now? (10-top, 1-bottom)"

* kk14: food insecurity
	tab kk14, m
	rename kk14 eatless

	replace eatless = "4" if eatless == "Sering"
	replace eatless = "3" if eatless == "Terkadang"
	replace eatless = "2" if eatless == "Jarang"
	replace eatless = "1" if eatless == "Tidak pernah"
	destring eatless, replace

	la def eatless 1 "Never" 2 "Rarely" 3 "Sometimes" 4 "Often"
	la val eatless eatless

	gen eatless_n = (eatless/4)

	gen eatless_often = eatless == 4 if !missing(eatless)
	gen eatless_some = eatless == 3 if !missing(eatless)
	gen eatless_rarely = eatless == 2 if !missing(eatless)
	gen eatless_never = eatless == 1 if !missing(eatless)

	foreach var of varlist eatless* {
	  replace `var' = .a if eatless == -99
	  la val `var' yesno
	}

	la var eatless_often  "Often eat less than usual in past 3 months due to money"
	la var eatless_some   "Sometimes eat less than usual in past 3 months due to money"
	la var eatless_rarely "Rarely eat less than usual in past 3 months due to money"
	la var eatless_never  "Never eat less than usual in past 3 months due to money"
	la var eatless        "Over the past 3 months, how often did you eat less due to lack of money?"

	summ eatless*

** kk15: consumption smoothing activities (multi-select)
	tab kk15, m
	rename kk15 consmooth

	gen consmooth_savings = strpos(consmooth, "Menggunakan tabungan") > 0
	gen consmooth_loans = strpos(consmooth, "Meminjam uang") > 0
	gen consmooth_sell = strpos(consmooth, "Menjual/menggadaikan barang yang dimiliki") > 0
	gen consmooth_paidless = strpos(consmooth, "Melakukan pekerjaan dengan gaji lebih rendah") > 0
	gen consmooth_freelance = strpos(consmooth, "Menjadi pekerja lepas") > 0
	gen consmooth_reduce = strpos(consmooth, "Mengurangi pengeluaran") > 0
	gen consmooth_gov_aid = strpos(consmooth, "Mendaftar program bantuan di kantor pemerintah") > 0
	gen consmooth_prakerja = strpos(consmooth, "kartu Prakerja") > 0
	gen consmooth_BLT_UMKM = strpos(consmooth, "BLT UMKM") > 0
	gen consmooth_move = strpos(consmooth, "Pindah tempat tinggal") > 0
	gen consmooth_NA = strpos(consmooth, "Tidak melakukan aktivitas apapun") > 0
	gen consmooth_other = strpos(consmooth, "Lainnya") > 0

	foreach var of varlist consmooth_* {
		replace `var' = .a if consmooth == "-99"
		replace `var' = . if missing(consmooth)
		la val `var' yesno
	}

	la var consmooth_savings      "Used savings to overcome financial difficulties"
	la var consmooth_loans        "Took out loans to overcome financial difficulties"
	la var consmooth_sell         "Sold assets to overcome financial difficulties"
	la var consmooth_paidless 	  "Took job that pays less to overcome financial difficulties"
	la var consmooth_freelance    "Did freelance work to overcome financial difficulties"
	la var consmooth_reduce       "Reduced spending to overcome financial difficulties"
	la var consmooth_gov_aid      "Applied for government aid to overcome financial difficulties"
	la var consmooth_prakerja     "Applied for Kartu Prakerja to overcome financial difficulties"
	la var consmooth_BLT_UMKM     "Applied for BLT micro and small business assistance (BPUM) to overcome financial difficulties"
	la var consmooth_move         "Moved locations to overcome financial difficulties"
	la var consmooth_NA           "Did not experience financial difficulties"
	la var consmooth_other        "Other activity to overcome financial difficulties"
	la var consmooth              "Over the past 3 months, what did you do to overcome financial difficulties?"

	summ consmooth_*
	drop consmooth

	rename kk15_10_text consmooth_text
	la var consmooth_text "Over the past 3 months, what did you do to overcome financial difficulties? (Other, open-ended)"


/*----------------------------------------------------*/
                  /* Section: ka */
/*----------------------------------------------------*/

* KA01 - KA16: Asset transacations
	tab ka01, m

	rename ka01 bike
	rename ka02 bike_buy_val
	rename ka03 bike_sell_val
	rename ka04 bike_lend_val

	rename ka05 devices
	rename ka06 devices_buy_val
	rename ka07 devices_sell_val
	rename ka08 devices_lend_val

	rename ka09 gold
	rename ka10 gold_buy_val
	rename ka11 gold_sell_val
	rename ka12 gold_lend_val

	rename ka13 livestock
	rename ka14	livestock_buy_val
	rename ka15 livestock_sell_val
	rename ka16 livestock_lend_val

	foreach var of varlist bike devices gold livestock {
		gen `var'_buy = strpos(`var', "Ya, membeli") > 0 if `var' != ""
		gen `var'_sell = strpos(`var', "Ya, menjual") > 0 if `var' != ""
		gen `var'_lend = strpos(`var', "Ya, menggadaikan") > 0 if `var' != ""
		gen `var'_none = strpos(`var', "Tidak pernah") > 0 if `var' != ""

		foreach dummy of varlist `var'_* {
			replace `dummy' = .a if `var' == "-99"
			la val `dummy' yesno
		}
	}

	foreach var in buy sell lend {
		egen any_`var' = rowtotal(*_`var')
			replace any_`var' = 1 if any_`var' > 0
			replace any_`var' = . if bike == "-99" & devices == "-99" & devices == "-99"& gold == "-99" & livestock == "-99"
			replace any_`var' = . if missing(bike_`var')

		la val any_`var' yesno
	}

	
	summ *buy *sell *lend *none

	la var bike_buy 			"Bought a motorbike"
	la var bike_sell			"Sold a motorbike"
	la var bike_lend			"Lent a motorbike"
	la var bike_none			"Didn't buy, sell or lend a motorbike"
	la var bike_buy_val 		"Value of Motorbike bought"
	la var bike_sell_val 		"Value of Motorbike sold"
	la var bike_lend_val 		"Value of Motorbike mortgaged"

	la var devices_buy 			"Bought devices"
	la var devices_sell			"Sold devices"
	la var devices_lend			"Lent devices"
	la var devices_none			"Didn't buy, sell or lend devices"
	la var devices_buy_val  	"Value of Electronic devices bought"
	la var devices_sell_val 	"Value of Electronic devices sold"
	la var devices_lend_val 	"Value of Electronic devices mortgaged"

	la var gold_buy 			"Bought gold or jewelry"
	la var gold_sell			"Sold gold or jewelry"
	la var gold_lend			"Lent gold or jewelry"
	la var gold_none			"Didn't buy, sell or lend gold or jewelry"
	la var gold_buy_val 		"Value of gold/jewelry bought"
	la var gold_sell_val 		"Value of gold/jewelry sold"
	la var gold_lend_val  		"Value of gold/jewelry mortgaged"

	la var livestock_buy 		"Bought livestock"
	la var livestock_sell		"Sold livestock"
	la var livestock_lend		"Lent livestock"
	la var livestock_none		"Didn't buy, sell or lend livestock"
	la var livestock_buy_val  	"Value of livestock bought"
	la var livestock_sell_val 	"Value of livestock sold"
	la var livestock_lend_val 	"Value of livestock mortgaged"

	la var any_buy 				"Bought any assets (bike, devices, gold, livestock)"
	la var any_sell				"Sold any assets (bike, devices, gold, livestock)"
	la var any_lend				"Lent any assets (bike, devices, gold, livestock)"

	drop bike devices gold livestock


/*----------------------------------------------------*/
                  /* Section: sk */
/*----------------------------------------------------*/

* sk01: Loans
	tab sk01, m
	rename sk01 loan

	gen loan_borrow = strpos(loan,"Meminjam uang") > 0
	gen loan_get = strpos(loan,"Menerima uang") > 0
	gen loan_lend = strpos(loan,"Meminjamkan uang") > 0
	gen loan_give = strpos(loan,"Memberikan uang") > 0
	gen loan_NA = strpos(loan,"Tidak melakukan") > 0

	foreach var of varlist loan_* {
		replace `var' = .a if loan == "-99"
		replace `var' = . if missing(loan)

		la val `var' yesno
	}

	la var loan_borrow      "Taken additional loans"
	la var loan_get  		"Received gifts"
	la var loan_lend        "Given loans"
	la var loan_give 		"Gave gifts"
	la var loan_NA          "Did not do any loan activites"
	la var loan             "Which of the following loan activities have you done?"

	summ loan_*

* sk02: rupiah amount variables
	local categories `" 1 "Kurang dari Rp 99 ribu" 2 "Rp 100 ribu - 249 ribu" 3 "Rp 250 ribu - 499 ribu" 4 "Rp 500 ribu - 999 ribu"	5  "Rp 1 juta - 2.49 juta" 6 "Rp 2.5 juta - 4.99 juta" 7 "Rp 5 juta - 9.99 juta" 8 "Lebih dari Rp 10 juta" "'

	tokenize `categories'
	la def categories `categories', replace

	rename sk02 loan_borrow_amt
	rename sk05 loan_get_amt
	rename sk07 loan_lend_amt
	rename sk09 loan_give_amt

	foreach var of varlist loan_borrow_amt loan_get_amt loan_lend_amt loan_give_amt {
		* fix slight difference in response text between questions
		replace `var' = "Lebih dari Rp 10 juta" if `var' == "Lebih dari 10 juta"
		replace `var' = "Rp 1 juta - 2.49 juta" if `var' == "Rp 1 juta - 2,49 juta"
		replace `var' = "Rp 2.5 juta - 4.99 juta" if `var' == "Rp 2,5 juta - 4,99 juta"
		replace `var' = "Rp 2.5 juta - 4.99 juta" if `var' == "Rp 2,5 juta - Rp 4,99 juta"
		replace `var' = "Rp 5 juta - 9.99 juta" if `var' == "Rp 5 juta - 9,99 juta"
		replace `var' = "Rp 5 juta - 9.99 juta" if `var' == "Rp 5 juta - 9,99"

		local x = 1
		local y = 2
		forval i = 1 / 8 {
			replace `var' = "``x''" if `var' == "``y''"
			local x = `x' + 2
			local y = `y' + 2
		}

		destring `var', replace
		replace `var' = .a if `var' == -99
		la val `var' categories
	}

	la var loan_borrow_amt 	"How much have you/your household borrowed?"
	la var loan_get_amt 	"What is the value of all the gifts that you/your household received?"
	la var loan_lend_amt 	"How much did you/your household lend?"
	la var loan_give_amt 	"How much did you/your household give, in gifts?"

* transaction medium
	tab sk04, m

	rename sk04 borrow
	rename sk06 get
	rename sk08 lend
	rename sk10 give

	foreach i of varlist borrow get lend give {
		gen `i'_cash = strpos(`i',"Melalui uang tunai") > 0 if !missing(`i')
		gen `i'_bank = strpos(`i',"Melalui transfer bank") > 0 if !missing(`i')
		gen `i'_ewallet = strpos(`i',"Melalui transfer e-wallet") > 0 if !missing(`i')
		gen `i'_item = strpos(`i',"barang fisik") > 0 if !missing(`i')
		gen `i'_other = strpos(`i',"Lainnya") > 0 if !missing(`i')

		foreach dummy of varlist `i'_* {
			replace `dummy' = .a if `i' == "-99"
			lab val `dummy' yesno
		}
	}

	la var borrow_cash 		"Method of borrowing money: cash"
	la var borrow_bank 		"Method of borrowing money: bank"
	la var borrow_ewallet 	"Method of borrowing money: e-wallet"
	la var borrow_item 		"Method of borrowing money: items"
	la var borrow_other 	"Method of borrowing money: other"

	la var get_cash 		"Method of receiving gifts: cash"
	la var get_bank 		"Method of receiving gifts: bank"
	la var get_ewallet 		"Method of receiving gifts: e-wallet"
	la var get_item 		"Method of receiving gifts: items"
	la var get_other 		"Method of receiving gifts: other"

	la var lend_cash 		"Method of giving out loans: cash"
	la var lend_bank 		"Method of giving out loans: bank"
	la var lend_ewallet 	"Method of giving out loans: e-wallet"
	la var lend_item 		"Method of giving out loans: items"
	la var lend_other 		"Method of giving out loans: other"

	la var give_cash 		"Method of giving gifts: cash"
	la var give_bank 		"Method of giving gifts: bank"
	la var give_ewallet 	"Method of giving gifts: e-wallet"
	la var give_item 		"Method of giving gifts: items"
	la var give_other 		"Method of giving gifts: other"

	summ *cash *bank *ewallet *item *other
	drop loan borrow get lend give

* sk03: from whom did you borrow
	tab sk03, m
	rename sk03 loan_from

	gen loan_from_family = strpos(loan_from, "Keluarga") > 0 if !missing(loan_from)
	gen loan_from_friends = strpos(loan_from, "Teman") > 0 if !missing(loan_from)
	gen loan_from_banks = strpos(loan_from, "Bank") > 0 if !missing(loan_from)
	gen loan_from_microfin = strpos(loan_from, "Lembaga keuangan mikro") > 0 if !missing(loan_from)
	gen loan_from_moneylend = strpos(loan_from, "Perorangan lainnya dengan bunga") > 0 if !missing(loan_from)
	gen loan_from_other = strpos(loan_from, "Lainnya") > 0 if !missing(loan_from)

	foreach i of varlist loan_from_* {
		replace `i' = .a if loan_from == "-99"
		la val `i' yesno
	}

	summ loan_from_*

	la var loan_from 			"From whom did you/your household borrow?"
	la var loan_from_family 	"Household borrowed from family"
	la var loan_from_friends 	"Household borrowed from friends"
	la var loan_from_banks 		"Household borrowed from banks"
	la var loan_from_microfin 	"Household borrowed from microfinance organization"
	la var loan_from_moneylend 	"Household borrowed from moneylenders"
	la var loan_from_other 		"Household borrowed from other"


/*----------------------------------------------------*/
                  /* Section: dk */
/*----------------------------------------------------*/

*dk01: work last week
	la var dk01 "Work for pay/assist business/own business in the past week"
	rename dk01 employed
	tab employed, m
			replace employed = . if employed==.a
			
* dk02: temporarily not working
	la var dk02 "Have a job/own a business, but not work in the past week"
	rename dk02 temp_no_work
	//replace temp_no_work = 0 if missing(temp_no_work)
	tab temp_no_work, m
			replace temp_no_work = . if temp_no_work==.a

* KBLI and KBJI codes (dk03-4 and dk19-20)
	foreach i of varlist dk03_? dk04_? {
		encode `i', gen(`i'_encode)
		replace `i'_encode = .a if `i' == "-99"
		drop `i'
		rename `i'_encode `i'
		tab `i', m
	}

	rename dk03_1 KBLI_1
	rename dk03_2 KBLI_2
	rename dk03_3 KBLI_3
	rename dk04_1 KBJI_1
	rename dk04_2 KBJI_2

	la var KBLI_1 "KBLI 1-digit main job (Industry Category)"
	la var KBLI_2 "KBLI 2-digit main job (Industry)"
	la var KBLI_3 "KBLI 2-digit main job (Sub-Industry)"
	la var KBJI_1 "KBJI 1-digit main job"
	la var KBJI_2 "KBJI 2-digit main job"

* dk05 - dk10: combine two branches of survey questions (randomized ordering of questions)
	foreach var of varlist dk05 dk06 dk08 dk10 dk11 dk12 dk13 dk15 {
		assert missing(`var') if !missing(`var'_v2)
		replace `var' = `var'_v2 if !missing(`var'_v2)
		drop `var'_v2
	}

	forval i = 1/2 {
		assert missing(dk07_`i') if !missing(dk07_v2_`i')
		replace dk07_`i' = dk07_v2_`i' if !missing(dk07_v2_`i')
		drop dk07_v2_`i'
	}

	forval i = 1/7 {
		foreach dk in dk09 dk14{
			assert missing(`dk'_1_`i') if !missing(`dk'_v2_1_`i')
			replace `dk'_1_`i' = `dk'_v2_1_`i' if !missing(`dk'_v2_1_`i')
			drop `dk'_v2_1_`i'
		}
	}

* dk05 and dk13: employment status
	tab dk05, m
	rename dk05 employ_status
	rename dk13 employ_status2

	label define emp 1 "Self-employed" ///
				 2 "Laborer/permanent employee" ///
				 3 "Laborer/temporary employee or freelance" ///
				 4 "Business owner" ///
				 5 "Family/unpaid worker"

	foreach var in employ_status employ_status2 {
		replace `var' = "1" if strpos(`var', "Berusaha sendiri") > 0
		replace `var' = "2" if strpos(`var', "Buruh/karyawan/pegawai tetap") > 0
		replace `var' = "3" if strpos(`var', "Buruh/karyawan/pegawai temporer atau") > 0
		replace `var' = "3" if strpos(`var', "Buruh/karyawan/pegawai lepas atau") > 0
		replace `var' = "4" if strpos(`var', "Pemilik Wirausaha") > 0
		replace `var' = "5" if strpos(`var', "Pekerja keluarga/tidak dibayar") > 0

		destring `var', replace
		replace `var' = .a if `var' == -99
		la val `var' emp
	}

	la var employ_status "Employment status in main job"
	la var employ_status2 "Employment status in second job"

	gen self_emp = employ_status == 1 if !missing(employ_status)
	gen bus_owner = employ_status == 4  if !missing(employ_status)
	gen perm_emp = employ_status == 2  if !missing(employ_status)
	gen temp_emp = employ_status == 3  if !missing(employ_status)
	gen fam_unpaid = employ_status == 5  if !missing(employ_status)
	gen self_emp_bus_owner = (employ_status == 1 | employ_status == 4) if !missing(employ_status)

	* missing if non-missing employed but missing the employ_status
	foreach var of varlist self_emp bus_owner perm_emp temp_emp fam_unpaid self_emp_bus_owner {
		replace `var' = . if !missing(employed) & employ_status==.
	}

	la val self_emp yesno
	la val bus_owner yesno
	la val perm_emp yesno
	la val temp_emp yesno
	la val fam_unpaid yesno
	la val self_emp_bus_owner yesno

	la var self_emp           "Self-employed"
	la var bus_owner          "Business owner"
	la var perm_emp           "Permanent employee"
	la var temp_emp           "Temporary employee"
	la var fam_unpaid     	  "Working with family/unpaid worker"
	la var self_emp_bus_owner "Self-employed OR Business owner"

* dk06 and dk15: income
	rename dk06 income
	la var income "Income from main job (in rupiah)"

	rename dk15 income2
	la var income2 "Income from secondary job (in rupiah)"

	egen inc_all = rowtotal(income income2) if !(mi(income) & mi(income2))
	replace inc_all = 0 if employed == 0

* Winsorize incomes
	gen income_adj = inc_all
	replace income_adj = inc_sak_u if income_adj > inc_sak_u & !mi(income_adj)
	replace income_adj = inc_sak_l if income_adj < inc_sak_l & !mi(income_adj) & income_adj !=0
	replace income_adj = income_adj/1000

	gen income_positive = income_adj > 0 if !mi(income_adj)

* create separate date variable (assume middle of month for start date average)
	gen job_startdate = mdy(dk07_1, 15, dk07_2)
	replace job_startdate = .a if dk07_2 == .a | dk07_1 == .a
	format job_startdate %td
	summ job_startdate, format
	la var job_startdate  "Start date of job or business activity"
	drop dk07*

* calculate current job tenure
	gen job_tenure = startdateDay - job_startdate
	la var job_tenure "Tenure of current job (in days)"

* dk08: period of job search (should we re-label more than 24 months as something else?)
	tab dk08, m
	rename dk08 job_search_time
	la var job_search_time "Months it took to find/prepare current main job/business"

* dk09 and dk14: hours worked
	summ dk09* dk14*

	local days = "mon tue wed thu fri sat sun"
	tokenize `days'

	forval i = 1/7 {
		gen jobhours_``i'' = dk09_1_`i'
		gen jobhours2_``i'' = dk14_1_`i'
	}

	drop dk09_1* dk14_1*

	* if respondent leaves one day blank but answers other days, assume this is a 0, not missing
	egen hour_nonmiss = rownonmiss(jobhours_*)

	foreach var of varlist jobhours_* {
		replace `var' = 0 if missing(`var') & hour_nonmiss > 0
	}

	egen hour_nonmiss2 = rownonmiss(jobhours2_*)
		replace hour_nonmiss2 = . if missing(jobhours2_mon)
		
	foreach var of varlist jobhours2_* {
		replace `var' = 0 if missing(`var') & hour_nonmiss2 > 0
	}

	drop hour_nonmiss*

	* total hours per week
	egen jobhours = rowtotal(jobhours*) , missing

	replace jobhours = .a if jobhours_mon == .a ///
							& jobhours_tue == .a ///
							& jobhours_wed == .a ///
							& jobhours_thu == .a ///
							& jobhours_fri == .a ///
							& jobhours_sat == .a ///
							& jobhours_sun == .a

	la var jobhours "Hours worked in main job in past week"

	egen jobhours2 = rowtotal(jobhours2*) , missing

	replace jobhours2 = .a if jobhours2_mon == .a ///
							& jobhours2_tue == .a ///
							& jobhours2_wed == .a ///
							& jobhours2_thu == .a ///
							& jobhours2_fri == .a ///
							& jobhours2_sat == .a ///
							& jobhours2_sun == .a

	la var jobhours2 "Hours worked in secondary job in past week"

	summ jobhours*
	summ jobhours2*

* total hours worked in last week among all jobs;
	egen jobhours_total = rowtotal(jobhours jobhours2) if !(mi(jobhours) & mi(jobhours2))
	replace jobhours_total = 0 if employed == 0
	sum jobhours_total, d

// 210 hours in a week is not possible, so we truncate
	gen jobhours_adj = jobhours_total
	*Winsorize
	replace jobhours_adj = hrs_sak_u if jobhours_adj > hrs_sak_u & !mi(jobhours_adj)
	sum jobhours_adj, d

	la var jobhours_adj "Total hours worked in the past week"

* wages
	gen wage = (income_adj/31)/(jobhours_adj/7)
	replace wage = 0 if jobhours_adj == 0
	sum wage, d
	gen wage_adj = wage
	*Winsorize
	replace wage_adj = wage_sak_u if wage_adj > wage_sak_u & !mi(wage_adj)
	replace wage_adj = wage_sak_l if wage_adj < wage_sak_l & !mi(wage_adj) & wage_adj !=0
	sum wage_adj, d

	gen wage_positive = wage_adj > 0 if !mi(wage_adj)



* dk10: job satisfaction
	tab dk10, m
	rename dk10 job_satisfy
	la var job_satisfy "Satisfaction with current main job (on a scale of 1-6, 1 = completely dissatisfied, 6 = completely satisfied)"

	gen job_satisfy_n = (job_satisfy/6)

* dk11: internet use at job
	tab dk11
	gen job_web_promo = strpos(dk11, "Ya, untuk promosi") > 0
	gen job_web_comm = strpos(dk11, "Ya, untuk komunikasi") > 0
	gen job_web_buy_sell = strpos(dk11, "Ya, untuk menjual/membeli barang") > 0
	gen job_web_info = strpos(dk11, "Ya, untuk mencari informasi online") > 0
	gen job_web_not_use = strpos(dk11, "Tidak, saya tidak menggunakan internet pada pekerjaan ini") > 0

	foreach var of varlist job_web_promo job_web_comm job_web_buy_sell job_web_info job_web_not_use {
		replace `var' = .a if dk11 == "-99"
		destring `var', replace
	}

	drop dk11
	summ job_web*

	la var job_web_promo "Use internet on the job for promotion"
	la var job_web_comm "Use internet on the job for communication"
	la var job_web_buy_sell "Use internet on the job to sell/buy items"
	la var job_web_info "Use internet on the job to search for information online"
	la var job_web_not_use "Don't use internet on the job"

	egen job_web = rowtotal(job_web_*)
	replace job_web = 1 if job_web > 0

	la var job_web "Use internet on the job"
	la val job_web yesno

* dk12: multiple jobs
	tab dk12, m
	rename dk12 multiple_jobs
	replace multiple_jobs = 0 if multiple_jobs == .
	la var multiple_jobs "Had more than one job in past week"
	tab multiple_jobs, m

* dk16: searching for job
	rename dk16 job_search
	la var job_search "In the past week, are you actively searching for a job?"
	tab job_search, m

* dk17: How many months have you been searching for a job?
	tab dk17, m
	rename dk17 job_search_time_curr
	la var job_search_time_curr "Months spent looking for job (if currently actively looking for a job)"

* dk18: What efforts are you making in your current job search?
	gen job_search_ads = strpos(dk18, "Mencari pekerjaan berdasarkan lowongan pekerjaan di media") > 0
	gen job_search_advert = strpos(dk18, "Mengiklankan diri sendiri di media") > 0
	gen job_search_company = strpos(dk18, "Menghubungi perusahaan/kantor") > 0
	gen job_search_friends = strpos(dk18, "Menghubungi orang terdekat") > 0
	gen job_search_capital = strpos(dk18, "Mengumpulkan modal/peralatan") > 0
	gen job_search_location = strpos(dk18, "Mencari lokasi bisnis") > 0

	foreach var of varlist job_search_ads job_search_advert job_search_company job_search_friends job_search_capital job_search_location {
		replace `var' = .a if dk18 == "-99"
		destring `var', replace
	}

	summ job_search_*

	la var job_search_ads "Looking for jobs based on advertisements in print/electronic/online media"
	la var job_search_advert "Advertised yourself in print/electronic/online media"
	la var job_search_company "Contacted company/office electronically/online for job"
	la var job_search_friends "Contacted people closest to you for job"
	la var job_search_capital "Collecting capital/equipment to start business"
	la var job_search_location "Looking for a business location"

	drop dk18

* dk19: Have you ever made use of online job platforms for your job search?
	rename dk19 job_search_online
	la var job_search_online "Used online job platforms for job search"
	tab job_search_online

* dk20: How comfortable are you in using said online job platform?
	rename dk20 job_online_comf
	la var job_online_comf "Comfort in using online job platforms (1 - not comfortable, 6 - very comfortable)?"
	tab job_online_comf, m

	gen job_online_comf_pos = job_online_comf > 3
	la var job_online_comf_pos "Comfortable to very comfortable with using online job platforms for job search"

* dk21: preparing to start new business
	rename dk21 prep_new_bus
	la var prep_new_bus "Preparing to start a new business in last week"
	tab prep_new_bus, m

* dk22: how long preparing to start new business
	tab dk22, m
	rename dk22 prep_new_bus_time
	la var prep_new_bus_time "Months spent prepping new business"

* dk23: HH member started business
	rename dk23 hh_new_bus
	la var hh_new_bus "Household member started a business"
	tab hh_new_bus, m

* dk24: when did your HH member's business start (assume middle of month for dates)
	assert missing(dk24_v1_1)
	assert missing(dk24_v2_1)

	gen hh_new_bus_start = mdy(dk24_1, 15, dk24_2)
	format hh_new_bus_start %td

	summ hh_new_bus_start, format
	la var hh_new_bus_start "Household member's new business start date'"

	drop dk24*


/*----------------------------------------------------*/
                  /* Section: re */
/*----------------------------------------------------*/

* re01: Why, in your opinion, are there people in this country who live in need?
	tab re01, m
	replace re01 = "1" if re01 == "Kemiskinan disebabkan oleh kemalasan dan kurangnya tekad/motivasi"
	replace re01 = "0" if re01 == "Kemiskinan disebabkan oleh kondisi sosial yang kurang adil"
	destring re01, replace
	replace re01 = .a if re01 == -99
	la var re01 "Why, in your opinion, are there people in this country who live in need?"
	la def re01 1 "Poor because of laziness and lack of willpower" 0 "Poor because of an unfair society", replace
	la val re01 re01
	rename re01 views_why_poor
	tab views_why_poor, m

* re02: The government vs each person in Indonesia should do everything to improve the standard of living of all poor Indonesian
	tab re02 re02_2, m
	gen views_gov_help = re02
	replace views_gov_help = re02_2 if missing(re02)
	tab views_gov_help, m
	drop re02 re02_2
	replace views_gov_help = 7-views_gov_help
	la var views_gov_help  "Government responsibility to help the poor (1 - Individual has highest responsibility, 6 - Govt has highest responsibility)"
	gen views_gov_help_n = views_gov_help/6

* re03: allocate social assistance budget to the poor and those most in need vs even distribution
	tab re03 re03_2, m
	gen views_allocation = re03
	replace views_allocation = re03_2 if missing(re03)
	tab views_allocation, m
	drop re03 re03_2
	replace views_allocation = 7-views_allocation
	la var views_allocation  "Allocation of social assistance (1 - equal distribution, 6 - poor get the most)"
	gen views_allocation_n = views_allocation/6

/*----------------------------------------------------*/
                  /* Section: tr */
/*----------------------------------------------------*/

* WILLINGNESS TO PAY (ONLINE TRAINING)
	tab tr01, m

* Note: 1 always equals the training voucher, while 0 equals the ewallet/bank transfer
*Label tr01-tr06: Online training Voucher
	la var tr01 "If the program were expanded, would you prefer: 1 mil online training voucher vs. 600K ewallet/bank transfer?"
	la var tr02 "If the program were expanded, would you prefer: 1 mil online training voucher vs. 800K ewallet/bank transfer?"
	la var tr03 "If the program were expanded, would you prefer: 1 mil online training voucher vs. 1 mil ewallet/bank transfer?"
	la var tr04 "If the program were expanded, would you prefer: 1 mil online training voucher vs. 1.2 mil ewallet/bank transfer?"
	la var tr05 "If the program were expanded, would you prefer: 1 mil online training voucher vs. 400K ewallet/bank transfer?"
	la var tr06 "If the program were expanded, would you prefer: 1 mil online training voucher vs. 200K ewallet/bank transfer?"

	la def tr01 1 "1 mil online training voucher" 0 "600K ewallet/bank transfer", modify
	la def tr02 1 "1 mil online training voucher" 0 "800K ewallet/bank transfer", modify
	la def tr03 1 "1 mil online training voucher" 0 "1 mil ewallet/bank transfer", modify
	la def tr04 1 "1 mil online training voucher" 0 "1.2 mil ewallet/bank transfer", modify
	la def tr05 1 "1 mil online training voucher" 0 "400K ewallet/bank transfer", modify
	la def tr06 1 "1 mil online training voucher" 0 "200K ewallet/bank transfer", modify

*Label tr07-tr12 V1: Offline training Voucher (5 mil base)
	la var tr07_v1 "If the program were expanded, would you prefer: 1 mil offline training scholarship vs. 600k ewallet/bank transfer?"
	la var tr08_v1 "If the program were expanded, would you prefer: 1 mil offline training scholarship vs. 800k ewallet/bank transfer?"
	la var tr09_v1 "If the program were expanded, would you prefer: 1 mil offline training scholarship vs. 1 mil ewallet/bank transfer?"
	la var tr10_v1 "If the program were expanded, would you prefer: 1 mil offline training scholarship vs. 1.2 mil ewallet/bank transfer?"
	la var tr11_v1 "If the program were expanded, would you prefer: 1 mil offline training scholarship vs. 400k ewallet/bank transfer?"
	la var tr12_v1 "If the program were expanded, would you prefer: 1 mil offline training scholarship vs. 200k ewallet/bank transfer?"

	la def tr07_v1 1 "1 mil offline training scholarship" 0 "600k ewallet/bank transfer", modify
	la def tr08_v1 1 "1 mil offline training scholarship" 0 "800k ewallet/bank transfer", modify
	la def tr09_v1 1 "1 mil offline training scholarship" 0 "1 mil ewallet/bank transfer", modify
	la def tr10_v1 1 "1 mil offline training scholarship" 0 "1.2 mil ewallet/bank transfer", modify
	la def tr11_v1 1 "1 mil offline training scholarship" 0 "400k ewallet/bank transfer", modify
	la def tr12_v1 1 "1 mil offline training scholarship" 0 "200k ewallet/bank transfer", modify

*Label tr07-tr12 V2: Offline training Voucher (1 mil base)
	la var tr07_v2 "If the program were expanded, would you prefer: 3.5 mil offline training scholarship vs. 2.5 mil ewallet/bank transfer?"
	la var tr08_v2 "If the program were expanded, would you prefer: 3.5 mil offline training scholarship vs. 3 mil ewallet/bank transfer?"
	la var tr09_v2 "If the program were expanded, would you prefer: 3.5 mil offline training scholarship vs. 3.5 mil ewallet/bank transfer?"
	la var tr10_v2 "If the program were expanded, would you prefer: 3.5 mil offline training scholarship vs. 4 mil ewallet/bank transfer?"
	la var tr11_v2 "If the program were expanded, would you prefer: 3.5 mil offline training scholarship vs. 2 mil ewallet/bank transfer?"
	la var tr12_v2 "If the program were expanded, would you prefer: 3.5 mil offline training scholarship vs. 1.5 mil ewallet/bank transfer?"

	la def tr07_v2 1 "3.5 mil offline training scholarship" 0 "2.5 mil ewallet/bank transfer", modify
	la def tr08_v2 1 "3.5 mil offline training scholarship" 0 "3 mil ewallet/bank transfer", modify
	la def tr09_v2 1 "3.5 mil offline training scholarship" 0 "3.5 mil ewallet/bank transfer", modify
	la def tr10_v2 1 "3.5 mil offline training scholarship" 0 "4 mil ewallet/bank transfer", modify
	la def tr11_v2 1 "3.5 mil offline training scholarship" 0 "2 mil ewallet/bank transfer", modify
	la def tr12_v2 1 "3.5 mil offline training scholarship" 0 "1.5 mil ewallet/bank transfer", modify

	forval i = 1/6 {
		replace tr0`i' = "1" if strpos(tr0`i', "voucher pelatihan") > 0
		replace tr0`i' = "0" if strpos(tr0`i', "transfer uang melalui") > 0
		destring tr0`i', replace
		replace tr0`i' = .a if tr0`i' == -99
		la val tr0`i' tr0`i'
		tab tr0`i', m
	}

	foreach i in 07 08 09 10 11 12 {
		forval j = 1/2 {
			replace tr`i'_v`j' = "1" if strpos(tr`i'_v`j', "voucher pelatihan") > 0
			replace tr`i'_v`j' = "0" if strpos(tr`i'_v`j', "transfer uang melalui") > 0
			destring tr`i'_v`j', replace
			replace tr`i'_v`j' = .a if tr`i'_v`j' == -99
			la val tr`i'_v`j' tr`i'_v`j'
			tab tr`i'_v`j', m
		}
	}

	rename tr06 voucher_on_1 // 1 mil online training voucher vs 200k ewallet/bank transfer
	rename tr05 voucher_on_2 // 1 mil online training voucher vs 400k ewallet/bank transfer
	rename tr01 voucher_on_3 // 1 mil online training voucher vs 600k ewallet/bank transfer
	rename tr02 voucher_on_4 // 1 mil online training voucher vs 800k ewallet/bank transfer
	rename tr03 voucher_on_5 // 1 mil online training voucher vs 1 mil ewallet/bank transfer
	rename tr04 voucher_on_6 // 1 mil online training voucher vs 1.2 mil ewallet/bank transfer

	rename tr12_v1 voucher_off_v1_1 // 1 mil offline training voucher vs 200k ewallet/bank transfer
	rename tr11_v1 voucher_off_v1_2 // 1 mil offline training voucher vs 400k ewallet/bank transfer
	rename tr07_v1 voucher_off_v1_3 // 1 mil offline training voucher vs 600k ewallet/bank transfer
	rename tr08_v1 voucher_off_v1_4 // 1 mil offline training voucher vs 800k ewallet/bank transfer
	rename tr09_v1 voucher_off_v1_5 // 1 mil offline training voucher vs 1 mil ewallet/bank transfer
	rename tr10_v1 voucher_off_v1_6 // 1 mil offline training voucher vs 1.2 mil ewallet/bank transfer

	rename tr12_v2 voucher_off_v2_1 // 3.5 mil offline training voucher vs 200k ewallet/bank transfer
	rename tr11_v2 voucher_off_v2_2 // 3.5 mil offline training voucher vs 400k ewallet/bank transfer
	rename tr07_v2 voucher_off_v2_3 // 3.5 mil offline training voucher vs 600k ewallet/bank transfer
	rename tr08_v2 voucher_off_v2_4 // 3.5 mil offline training voucher vs 800k ewallet/bank transfer
	rename tr09_v2 voucher_off_v2_5 // 3.5 mil offline training voucher vs 1 mil ewallet/bank transfer
	rename tr10_v2 voucher_off_v2_6 // 3.5 mil offline training voucher vs 1.2 mil ewallet/bank transfer

	tab voucher_on_5
	tab voucher_off_v1_5
	tab voucher_off_v2_5

	gen wtp_online_training = .
	replace wtp_online_training = 0.1 if voucher_on_1 ==0
	replace wtp_online_training = 0.3 if voucher_on_1 == 1
	replace wtp_online_training = 0.5 if voucher_on_2 == 1
	replace wtp_online_training = 0.7 if voucher_on_4 == 0
	replace wtp_online_training = 0.9 if voucher_on_5 == 0
	replace wtp_online_training = 1.1 if voucher_on_6 == 0
	replace wtp_online_training = 1.3 if voucher_on_6 ==1

	gen wtp_offline_training1 = .
	replace wtp_offline_training1 = 0.1 if voucher_off_v1_1 ==0
	replace wtp_offline_training1 = 0.3 if voucher_off_v1_1 == 1
	replace wtp_offline_training1 = 0.5 if voucher_off_v1_2 == 1
	replace wtp_offline_training1 = 0.7 if voucher_off_v1_4 == 0
	replace wtp_offline_training1 = 0.9 if voucher_off_v1_5 == 0
	replace wtp_offline_training1 = 1.1 if voucher_off_v1_6 == 0
	replace wtp_offline_training1 = 1.3 if voucher_off_v1_6 ==1

	gen wtp_offline_training2 = .
	replace wtp_offline_training2 = 1/35 if voucher_off_v1_1 ==0
	replace wtp_offline_training2 = 3/35 if voucher_off_v1_1 == 1
	replace wtp_offline_training2 = 1/7 if voucher_off_v1_2 == 1
	replace wtp_offline_training2 = 1/5 if voucher_off_v1_4 == 0
	replace wtp_offline_training2 = 9/35 if voucher_off_v1_5 == 0
	replace wtp_offline_training2 = 11/35 if voucher_off_v1_6 == 0
	replace wtp_offline_training2 = 13/35 if voucher_off_v1_6 ==1

* tr13: Current training history
	rename tr13 take_courses
	replace take_courses = 1 if tr14==1
	la var take_courses "Currently taking training courses"
	tab take_courses, m

* tr14: kp-Specific training history
	rename tr14 take_kp_courses
	la var take_kp_courses "Taken Kartu Prakerja courses"
	tab take_kp_courses, m

* tr15: Number of courses taken
	cap drop num_courses
	tab tr15
	rename tr15 num_courses
	destring num_courses, replace
	replace num_courses = 0 if missing(num_courses)
	replace num_courses = .a if num_courses == -99

	la var num_courses "Number of courses taken"

* COURSE LOOP QUESTIONS

	* Define labels
	la def tr16_`i' 1 "Engineering" ///
					2 "IT" ///
					3 "Finance" ///
					4 "Office Admin" ///
					5 "Management" ///
					6 "Lifestyle" ///
					7 "F&B" ///
					8 "Sales&Marketing" ///
					9 "Foreign language" ///
					10 "Sociobehavioral" ///
					95 "Others", replace

	la def course_status 1 "Completed" ///
						2 "Ongoing" ///
						3 "Didn't Finish'", replace

	la def tr21_`i' 1 "Employers" ///
				2 "Govt" ///
				3 "kartu Prakerja" ///
				4 "Personal" ///
				5 "Relative" ///
				6 "Credit" ///
				7 "Free" ///
				95 "Others", replace

	la def tr23_`i' 1 "Online, with no live interaction" ///
				2 "Online, with live interaction" ///
				3 "In person", replace

	* Loop through each set of questions for each course
	forval i = 1/5 {

	* tr16: What is the category of the course you took?
		tab tr16_`i', m
		replace tr16_`i'="1" if strpos(tr16_`i',"Teknik") > 0
		replace tr16_`i'="2" if strpos(tr16_`i',"Teknologi") > 0
		replace tr16_`i'="3" if strpos(tr16_`i',"Keuangan") > 0
		replace tr16_`i'="4" if strpos(tr16_`i',"Perkantoran") > 0
		replace tr16_`i'="5" if strpos(tr16_`i',"Manajemen") > 0
		replace tr16_`i'="6" if strpos(tr16_`i',"Gaya") > 0
		replace tr16_`i'="7" if strpos(tr16_`i',"Pertanian") > 0
		replace tr16_`i'="8" if strpos(tr16_`i',"Makanan") > 0
		replace tr16_`i'="9" if strpos(tr16_`i',"Penjualan") > 0
		replace tr16_`i'="10" if strpos(tr16_`i',"Bahasa Asing") > 0
		replace tr16_`i'="11" if strpos(tr16_`i',"Sosial") > 0
		replace tr16_`i'="95" if strpos(tr16_`i',"Lainnya") > 0
		destring tr16_`i', replace
		replace tr16_`i' = .a if tr16_`i' == -99

		la val tr16_`i' tr16_`i'
		la var tr16_`i' "Course `i': Category"
		tab tr16_`i', m
		rename tr16_`i' course`i'_cat

	* tr17: Approximately how many hours total did you spend in this course?
		destring tr17_`i', replace force
		replace tr17_`i' = .a if tr17_`i' == -99
		la var tr17_`i' "Course `i': hours spent"
		rename tr17_`i' course`i'_hours

	* tr18: Did you complete this course?
		replace tr18_`i' = "1" if strpos(tr18_`i', "Ya") > 0
		replace tr18_`i' = "2" if strpos(tr18_`i', "Belum, pelatihan/kursus/training") > 0
		replace tr18_`i' = "3" if strpos(tr18_`i', "Tidak, saya tidak") > 0
		destring tr18_`i', replace
		replace tr18_`i' = .a if tr18_`i' == -99

		la val tr18_`i' course_status
		la var tr18_`i' "Course `i': completion status"
		rename tr18_`i' course`i'_status
		tab course`i'_status

		gen course`i'_complete = course`i'_status == 1
		la var course`i'_complete "Course `i': completed"
		la val course`i'_complete yesno

	* tr19: Did this course have a certificate?
		la var tr19_`i' "Course `i': course have a certificate"
		rename tr19_`i' course`i'_cert
		tab course`i'_cert, m

	* tr20: Have you used this certificate when you looked for a job?
		la var tr20_`i' "Course `i': used course certificate when looking for job"
		rename tr20_`i' course`i'_cert_use
		tab course`i'_cert_use, m

	* tr21: How did you primarily pay for this course?
		tab tr21_`i', m
		replace tr21_`i'="1" if strpos(tr21_`i',"tempat kerja") > 0
		replace tr21_`i'="2" if strpos(tr21_`i',"Pemerintah") > 0
		replace tr21_`i'="3" if strpos(tr21_`i',"Kartu Prakerja") > 0
		replace tr21_`i'="4" if strpos(tr21_`i',"pribadi") > 0
		replace tr21_`i'="5" if strpos(tr21_`i',"keluarga") > 0
		replace tr21_`i'="6" if strpos(tr21_`i',"Pinjaman") > 0
		replace tr21_`i'="7" if strpos(tr21_`i',"tidak berbayar") > 0
		replace tr21_`i'="95" if strpos(tr21_`i',"Lainnya") > 0
		destring tr21_`i', replace
		replace tr21_`i' = .a if tr21_`i' == -99
		la val tr21_`i' tr21_`i'
		la var tr21_`i' "Course `i': payment method"
		rename tr21_`i' course`i'_pay
		tab course`i'_pay, m

		gen course`i'_pay_kp = course`i'_pay == 3
		la var course`i'_pay_kp "Course `i': payment method was Kartu Prakerja"
		la val course`i'_pay_kp yesno

	* tr22: What was the cost of this course?
		lab var tr22_`i' "Course `i': cost (rupiah)"
		rename tr22_`i' course`i'_cost
		sum course`i'_cost, d

	* tr23: Was this course taken online or in person?
		tab tr23_`i', m
		replace tr23_`i'="1" if strpos(tr23_`i',"tanpa interaksi langsung") > 0
		replace tr23_`i'="2" if strpos(tr23_`i',"dengan interaksi langsung") > 0
		replace tr23_`i'="3" if strpos(tr23_`i',"Offline/tatap muka") > 0
		destring tr23_`i', replace
		replace tr23_`i' = .a if tr23_`i' == -99
		la val tr23_`i' tr23_`i'
		la var tr23_`i' "Course `i': delivery method"
		rename tr23_`i' course`i'_method
		tab course`i'_method, m

		gen course`i'_online = course`i'_method == 1
		gen course`i'_online_live = course`i'_method == 2
		gen course`i'_offline = course`i'_method == 3

		la var course`i'_online "Course `i': took online with no live interaction"
		la val course`i'_online yesno

		la var course`i'_online_live "Course `i': took online with live interaction"
		la val course`i'_online_live yesno

		la var course`i'_offline "Course `i': took in person"
		la val course`i'_offline yesno

	* tr24: On a scale of 1 - 6, what overall rating would you give this course in terms of its usefulness?
		tab tr24_`i', m
		la var tr24_`i' "Course `i': rating"
		rename tr24_`i' course`i'_rating

	* tr25: Did you take another course?
		capture rename tr25_`i' course`i'_more
		capture replace course`i'_more = tr25_`i'_check if tr25_`i'_check == 1
		capture drop tr25_`i'_check
		capture la var course`i'_more "Course `i': took another course prior to this course"

	}

	egen course_completed = rowtotal(course*_complete)
	replace course_completed = 1 if course_completed > 0
	la var course_completed "Completed at least one course"
	la val course_completed yesno

	egen course_cert = rowtotal(course*_cert)
	replace course_cert = 1 if course_cert > 0
	la var course_cert "Took a course that gave a course certificate"
	la val course_cert yesno

	egen course_cert_use = rowtotal(course*_cert_use)
	replace course_cert_use = 1 if course_cert_use > 0
	la var course_cert_use "Used any course certificate when looking for a job"
	la val course_cert_use yesno

	egen course_pay_kp = rowtotal(course*_pay_kp)
	replace course_pay_kp = 1 if course_pay_kp > 0
	la var course_pay_kp "Used Kartu Prakerja to pay for any course"
	la val course_pay_kp yesno

	egen course_online = rowtotal(course*_online)
	replace course_online = 1 if course_online > 0
	la var course_online "Took at least one course online"
	la val course_online yesno

	egen course_online_live = rowtotal(course*_online_live)
	replace course_online_live  = 1 if course_online_live > 0
	la var course_online_live "Took at least one course online with live instruction"
	la val course_online_live yesno

	egen course_offline = rowtotal(course*_offline)
	replace course_offline  = 1 if course_offline > 0
	la var course_offline "Took at least one course offline"
	la val course_offline yesno

	egen course_hours = rowtotal(course*hours)
	la var course_hours "Hours of Training"

/*----------------------------------------------------*/
                  /* Section: gf */
/*----------------------------------------------------*/

* gf01-gf08: Comfort with utilizing e-wallet and bank account
	la var gf01 "Comfort with: Signing up for a new e-wallet account (1-not comfortable, 6-very comfortable)"
	la var gf02 "Comfort with: Depositing/withdrawing to/from e-wallet (1-not comfortable, 6-very comfortable)"
	la var gf03 "Comfort with: Using an e-wallet for online transactions (1-not comfortable, 6-very comfortable)"
	la var gf04 "Comfort with: Using e-wallet for transactions with agent/shop (1-not comfortable, 6-very comfortable)"
	la var gf05 "Comfort with: Signing up for a new bank account (1-not comfortable, 6-very comfortable)"
	la var gf06 "Comfort with: Depositing/withdrawing to/from bank account (1-not comfortable, 6-very comfortable)"
	la var gf07 "Comfort with: Using a debit card for online transactions (1-not comfortable, 6-very comfortable)"
	la var gf08 "Comfort with: Using debit card for transactions with agent/shop (1-not comfortable, 6-very comfortable)"

* gf09: between e-wallets and bank accounts, which is more secure
* .d for don't know
	tab gf09, m
	replace gf09 = "1" if gf09 == "E-wallet jauh lebih aman daripada rekening bank"
	replace gf09 = "2" if gf09 == "E-wallet sedikit lebih aman daripada rekening bank"
	replace gf09 = "3" if gf09 == "Rekening bank sedikit lebih aman daripada e-wallet"
	replace gf09 = "4" if gf09 == "Rekening bank jauh lebih aman daripada e-wallet"
	replace gf09 = ".d" if gf09 == "Tidak tahu"
	destring gf09, replace
	replace gf09 = .a if gf09 == -99

	la def gf09 1 "E-wallets are much more secure" ///
				2 "E-wallets are somewhat more secure" ///
				3 "Bank accounts are somewhat more secure" ///
				4 "Bank accounts are much more secure", replace

	la val gf09 gf09
	la var gf09 "What is more secure: e-wallets or banks"
	rename gf09 ewallet_bank_secure
	tab ewallet_bank_secure, m

* E-wallet more secure dummy
	gen ewallet_secure = 0 if ewallet_bank_secure != .a
	replace ewallet_secure = 1 if ewallet_bank_secure == 1 | ewallet_bank_secure == 2
	tab ewallet_secure

* Bank more secure dummy
	gen bank_secure = 0 if ewallet_bank_secure != .a
	replace bank_secure = 1 if ewallet_bank_secure == 3 | ewallet_bank_secure == 4
	tab bank_secure

* gf10: Ownership of financial account
	tab gf10, m
	gen ewallet_own = strpos(gf10,"e-wallet") > 0
	gen bank_own = strpos(gf10,"bank") > 0
	gen neither_own = strpos(gf10,"Tidak memiliki keduanya") > 0

	foreach i of varlist *_own {
		replace `i' = .a if gf10 == "-99"
		la val `i' yesno
		replace `i' = . if missing(gf10)

	}

	la var ewallet_own		 "Own E-Wallet"
	la var bank_own 		 "Own Bank Account"
	la var neither_own 		 "Own Neither E-Wallet nor Bank Account"

	drop gf10

* gf11: Ownership of e-wallet account types
	tab gf11, m
	gen own_jenius = strpos(gf11,"Jenius") > 0
	gen own_ovo = strpos(gf11,"OVO") > 0
	gen own_linkaja = strpos(gf11,"LinkAja") > 0
	gen own_gopay = strpos(gf11,"GoPay") > 0
	gen own_dana = strpos(gf11,"Dana") > 0
	gen own_shopeepay = strpos(gf11,"ShopeePay") > 0
	gen own_others = strpos(gf11,"Lainnya") > 0

	foreach i of varlist own_gopay own_ovo own_linkaja own_dana own_jenius own_shopeepay own_others {
		replace `i' = .a if gf11 == "-99"
		replace `i' = 0 if ewallet_own == 0
		la val `i' yesno
	}

	summ own_*
	la var own_gopay 		"Own E-Wallet: GoPay"
	la var own_ovo 			"Own E-Wallet: OVO"
	la var own_linkaja 		"Own E-Wallet: LinkAja"
	la var own_dana 		"Own E-Wallet: Dana"
	la var own_jenius 		"Own E-Wallet: Jenius"
	la var own_shopeepay 	"Own E-Wallet: ShopeePay"
	la var own_others 		"Own E-Wallet: Others"

	drop gf11

* gf12 & gf22: Purchasing type using e-wallet/bank account
	rename gf12 ewallet
	rename gf22 bank

	foreach v in ewallet bank {
		tab `v', m
		gen `v'_buy_online = strpos(`v',"Ya, untuk membeli barang/jasa online") > 0
		gen `v'_buy_offline = strpos(`v',"Ya, untuk membeli barang/jasa secara tatap muka") > 0
		gen `v'_buy_never = strpos(`v',"Tidak menggunakan") > 0

		foreach i of varlist `v'_* {
			replace `i' = .a if `v' == "-99"
			replace `i' = 0 if `v'_own == 0
			la val `i' yesno
		}

	la var `v'_buy_online "`v': online purchases"
	la var `v'_buy_offline "`v': in-person purchases"
	la var `v'_buy_never "`v': no purchases"

	gen `v'_buy = 0
	replace `v'_buy = 1 if `v'_buy_online == 1 | `v'_buy_offline == 1
	la val `v'_buy yesno
	la var `v'_buy "`v': made online or offline purchase"
	}

	summ bank_* ewallet_*
	drop ewallet bank

* gf13&gf23: Paid bill services using e-wallet/bank account
	rename gf13 ewallet
	rename gf23 bank

	foreach v in ewallet bank {
		tab `v', m
		gen `v'_bill_mtg = strpos(`v',"Ya, untuk membayar cicilan") > 0
		gen `v'_bill_vphone = strpos(`v',"Ya, untuk membayar tagihan (listrik, air) melalui smartphone") > 0
		gen `v'_bill_vagent = strpos(`v',"Ya, untuk membayar tagihan (listrik, air) melalui agen") > 0
		gen `v'_bill_vbank = strpos(`v',"Ya, untuk membayar tagihan (listrik, air) melalui bank atau ATM") > 0
		gen `v'_bill_never = strpos(`v',"Tidak menggunakan") > 0

		foreach i of varlist `v'_* {
			replace `i' = .a if `v' == "-99"
			replace `i' = 0 if `v'_own == 0
			la val `i' yesno
		}

		egen `v'_bill_any = rowtotal(`v'_bill_*)
		replace `v'_bill_any = 1 if `v'_bill_any > 0

		la var `v'_bill_any "`v': pay any bill"
		la var `v'_bill_mtg "`v': payment of mortgage installments"
		la var `v'_bill_vphone "`v': payment of electricity/water bills via mobile phone"
		la var `v'_bill_vagent "`v': payment of electricity/water bills at remote banking agent"
		la var `v'_bill_vbank "`v': payment of electricity/water bills at a bank or ATM"
		la var `v'_bill_never "`v': No bills paid"
	}

	summ bank_* ewallet_*
	drop ewallet bank

	la val ewallet_bill_any yesno
	la val bank_bill_any yesno

* gf14&gf24: Utilization of bank feature in e-wallet/bank account
	rename gf14 ewallet
	rename gf24 bank

	foreach v in ewallet bank {
		tab `v', m
		gen `v'_ft_save = strpos(`v',"Tabungan/Investasi") > 0
		gen `v'_ft_insur = strpos(`v',"Pembelian asuransi") > 0
		gen `v'_ft_goods = strpos(`v',"Menerima pembayaran dari orang lain sebagai hasil penjualan") > 0
		gen `v'_ft_send = strpos(`v',"Memberi uang kepada orang lain") > 0
		gen `v'_ft_get = strpos(`v',"Menerima kiriman uang dari orang lain") > 0
		gen `v'_ft_wage = strpos(`v',"Menerima gaji dan/atau tunjangan") > 0
		gen `v'_ft_deposit = strpos(`v',"Menyetor/menyimpan uang tunai") > 0
		gen `v'_ft_wdraw = strpos(`v',"Menarik uang tunai") > 0
		gen `v'_ft_none = strpos(`v',"Tidak menggunakan") > 0

		foreach i of varlist `v'_* {
			replace `i' = .a if `v' == "-99"
			replace `i' = 0 if `v'_own == 0
			la val `i' yesno
		}

		egen `v'_ft_any = rowtotal(`v'_ft_*)
		replace `v'_ft_any = 1 if `v'_ft_any > 0

		la var `v'_ft_any "`v': use any feature"
		la var `v'_ft_save "`v': use account for Savings/Investment"
		la var `v'_ft_insur "`v': use account to Purchase Insurance"
		la var `v'_ft_goods "`v': use account for receiving payments for sales of Goods and Services"
		la var `v'_ft_send "`v': use account for Sending money to others"
		la var `v'_ft_get "`v': use account for receiving money from others"
		la var `v'_ft_wage "`v': use account for receiving wage payments"
		la var `v'_ft_deposit "`v': use account for Making deposits into the account"
		la var `v'_ft_wdraw "`v': use account for Withdrawing cash from the account"
		la var `v'_ft_none "`v': use account for No, I have not used any features"
	}

	summ bank_* ewallet_*
	drop ewallet bank

	la val ewallet_ft_any yesno
	la val bank_ft_any yesno

* gf15&gf25: Total transaction using e-wallet/bank account
	tab gf25 , m
	la def trx_freq 1 "Never" ///
					2 "Once" ///
					3 "A few times a month" ///
					4 "A few times a week" ///
					5 "Almost every day" ///
					6 "Every day" ///
					7 "Multiple times every day", replace

	foreach var in gf15 gf25 {
		replace `var' = "1" if `var' == "Tidak pernah"
		replace `var' = "2" if `var' == "Satu kali"
		replace `var' = "2" if `var' == "Sekali satu kali"
		replace `var' = "3" if `var' == "Beberapa kali dalam sebulan"
		replace `var' = "4" if `var' == "Beberapa kali dalam seminggu"
		replace `var' = "5" if `var' == "Hampir setiap hari"
		replace `var' = "6" if `var' == "Setiap hari"
		replace `var' = "7" if `var' == "Beberapa kali setiap hari"
		destring `var', replace
		replace `var' = .a if `var' == -99
		la val `var' trx_freq
	}

	rename gf15 ewallet_trans
	la var ewallet_trans "How many e-wallet transactions did you make in the past month?"

	gen ewallet_one_trans = ewallet_trans > 1
	replace ewallet_one_trans = 0 if ewallet_own == 0
	la val ewallet_one_trans yesno
	la var ewallet_one_trans "Made at least one e-wallet transaction in the last month"

	gen ewallet_multi_trans = ewallet_trans > 2
	replace ewallet_multi_trans = 0 if ewallet_own == 0
	la val ewallet_multi_trans yesno
	la var ewallet_multi_trans "Made multiple e-wallet transactions in the last month"

	rename gf25 bank_trans
	la var bank_trans "How many bank/debit card transactions did you make in the past month?"

	gen bank_one_trans = bank_trans > 1
	replace bank_one_trans = 0 if bank_own == 0
	la val bank_one_trans yesno
	la var bank_one_trans "Made at least one bank transaction in the last month"

	gen bank_multi_trans = bank_trans > 2
	replace bank_multi_trans = 0 if bank_own == 0
	la val bank_multi_trans yesno
	la var bank_multi_trans "Made multiple bank transactions in the last month"

* gf16: How do you fill your e-wallet
	tab gf16, m
	gen ewallet_top_bank  = strpos(gf16, "Menggunakan akun bank") > 0
	gen ewallet_top_agent = strpos(gf16, "Melalui agen") > 0
	gen ewallet_top_app   = strpos(gf16, "Melalui aplikasi") > 0
	gen ewallet_top_never = strpos(gf16, "Saya belum pernah") > 0

	foreach var of varlist ewallet_top_* {
		replace `var' = .a if gf16 == "-99"
		replace `var' = 0 if ewallet_own == 0
		la val `var' yesno
	}

	summ ewallet_top_*

	la var ewallet_top_bank 	"E-Wallet top-up method: Bank"
	la var ewallet_top_agent 	"E-Wallet top-up method: Agent"
	la var ewallet_top_app 		"E-Wallet top-up method: App"
	la var ewallet_top_never 	"E-Wallet top-up method: Never"

	drop gf16

* gf17: Ownership of bank account types
	tab gf17, m

	gen ewallet_himbara  = strpos(gf17,"Himbara") > 0
	gen ewallet_regional = strpos(gf17,"Pembangunan") > 0
	gen ewallet_private  = strpos(gf17,"Swasta") > 0

	foreach i of varlist ewallet_himbara ewallet_regional ewallet_private {
		replace `i' = .a if gf17 == "-99"
		replace `i' = 0 if ewallet_own == 0
		la val `i' yesno
	}

	la var ewallet_himbara  	"Own bank account in state-owned bank"
	la var ewallet_regional 	"Own bank account in regional bank"
	la var ewallet_private  	"Own bank account in State-owned bank"
	drop gf17

* gf18:  converted your e-wallet balance into cash?
* gf26: ever withdrawn cash from your bank account or from ATM?
	foreach var of varlist gf18 gf26 {
		tab `var', m
		replace `var' = "1" if `var' == "Pernah"
		replace `var' = "0" if `var' == "Belum Pernah"
		replace `var' = "0" if `var' == "Belum pernah"
		destring `var', replace
		replace `var' = .a if `var' == -99
		la val `var' yesno
	}

	rename gf18 ewallet_cash
	replace ewallet_cash = 0 if ewallet_cash == .
	la var ewallet_cash "Have converted e-wallet balance into cash"

	rename gf26 bank_cash
	replace bank_cash = 0 if bank_cash == .
	la var bank_cash "Have withdrawn cash from bank account or from ATM"

*GF19 and GF27: when did you last convert your e-wallet/bank account money into cash?
	gen ewallet_cash_time = mdy(gf19_1,15,gf19_2)
	format ewallet_cash_time %td
	la var ewallet_cash_time "When was the last time you converted your ewallet balance into cash?"

	gen bank_cash_time = mdy(gf27_1,15,gf27_2)
	la var bank_cash_time "When was the last time you converted your bank account balance into cash?"
	format bank_cash_time %td

	drop gf19* gf27*

* gf20: Amount withdrew last time from ewallet
	rename gf20_7_text ewallet_cash_amt
	replace ewallet_cash_amt = .d if strpos(gf20, "Tidak tahu") > 0
	replace ewallet_cash_amt = .a if ewallet_cash_amt == -99
	drop gf20
	la var ewallet_cash_amt "Amount withdrawn from e-wallet last time (rupiah)"

* gf28: Amount withdrew last time from bank account
	rename gf28_4_text bank_cash_amt
	replace bank_cash_amt = .d if strpos(gf28, "Tidak tahu") > 0
	replace bank_cash_amt = .a if bank_cash_amt == -99
	drop gf28
	la var bank_cash_amt "Amount withdrawn from bank account last time (rupiah)"

* gf21 & gf29: Place where cash was withdrawn from e-wallet/bank account
	rename gf21 ewallet
	rename gf29 bank

	foreach var of varlist ewallet bank {
		tab `var', m
		gen `var'_wd_atm = strpos(`var', "ATM") > 0
		gen `var'_wd_bank = strpos(`var', "Kantor cabang Bank") > 0
		gen `var'_wd_agent = strpos(`var', "Agen Perbankan") > 0
		gen `var'_wd_store = strpos(`var', "Toko serba ada") > 0
		replace `var'_wd_store = 1 if strpos(`var', "Toko Serba Ada") > 0
		gen `var'_wd_no = strpos(`var', "Tidak") > 0
		gen `var'_wd_others = strpos(`var', "Lainnya") > 0

		foreach i of varlist `var'_* {
			replace `i' = .a if `var' == "-99"
			replace `i' = 0 if `var'_own == 0
			la val `i' yesno
		}

		la var `var'_wd_atm 		"Cash withdrawn at ATM"
		la var `var'_wd_bank 		"Cash withdrawn at bank"
		la var `var'_wd_agent 		"Cash withdrawn with remote agent"
		la var `var'_wd_store 		"Cash withdrawn at store"
		la var `var'_wd_no 		"Did not withdraw cash"
		la var `var'_wd_others 		"Cash withdrawn at other place"
	}

	drop ewallet bank

* gf30: Online purchases (other than for Prakerja) in the last 3 months
	tab gf30, m

	gen purch_tokopedia = strpos(gf30,"Tokopedia") > 0
	gen purch_bukalapak = strpos(gf30,"Bukalapak") > 0
	gen purch_pintaria = strpos(gf30,"Pintaria") > 0
	gen purch_pijar = strpos(gf30,"Pijar Mahir") > 0
	gen purch_maubelajar = strpos(gf30,"Mau Belajar Apa") > 0
	gen purch_sekolahmu = strpos(gf30,"Sekolahmu") > 0
	gen purch_shopee = strpos(gf30,"Shopee") > 0
	gen purch_never = strpos(gf30,"Tidak melakukan transaksi pembelian di platform online") > 0
	gen purch_others = strpos(gf30,"Lainnya") > 0

	foreach var of varlist purch_* {
		replace `var' = .a if gf30 == "-99"
		la val `var' yesno
	}

	summ purch_*

	la var purch_tokopedia  	"Online purchases in last 3 months: Tokopedia"
	la var purch_bukalapak  	"Online purchases in last 3 months: Bukalapak"
	la var purch_pintaria   	"Online purchases in last 3 months: Pintaria"
	la var purch_pijar      	"Online purchases in last 3 months: Pijar Mahir"
	la var purch_maubelajar 	"Online purchases in last 3 months: Mau Belajar Apa"
	la var purch_sekolahmu  	"Online purchases in last 3 months: Sekolahmu"
	la var purch_shopee     	"Online purchases in last 3 months: Shopee"
	la var purch_never      	"Online purchases in last 3 months: No transaction"
	la var purch_others     	"Online purchases in last 3 months: Others"

	drop gf30

	local eb_list own ///
				  buy ///
				  buy_online ///
				  buy_offline ///
				  bill_any ///
				  bill_mtg ///
				  bill_vphone ///
				  bill_vagent ///
				  bill_vbank ///
				  cash ///
				  wd_atm ///
				  wd_bank ///
				  wd_agent ///
				  wd_store ///
				  one_trans ///
				  multi_trans ///
				  ft_any ///
				  ft_save ///
				  ft_insur ///
				  ft_goods ///
				  ft_send ///
				  ft_get ///
				  ft_wage ///
				  ft_deposit ///
				  ft_wdraw


	foreach i in `eb_list' {
		gen eb_`i' = 0
		replace eb_`i' = 1 if ewallet_`i' == 1 | bank_`i' == 1
		la val eb_`i' yesno
	}


/*----------------------------------------------------*/
                  /* Section: ps */
/*----------------------------------------------------*/

*ps01: depression
	tab ps01, m
	la def depression 1 "Never" 2 "Several days" 3 "More than half the days" 4 "Nearly every day", replace

	foreach var of varlist ps01 ps02 {
		assert missing(`var') if !missing(`var'_a)
		replace `var' = `var'_a if !missing(`var'_a)
		replace `var' = "1" if `var' == "Tidak pernah"
		replace `var' = "2" if `var' == "Beberapa hari"
		replace `var' = "3" if `var' == "Lebih dari setengah waktu, dalam 2 minggu terakhir"
		replace `var' = "4" if `var' == "Hampir setiap hari"
		destring `var', replace
		replace `var' = .a if `var' == -99
		la val `var' depression
		tab `var', m
	}

	rename ps01 depressed
	la var depressed "Over the last 2 weeks, how often have you felt down, depressed, or hopeless?"

	rename ps02 disinterested
	la var disinterested "In the last 2 weeks, how often did you feel disinterested/uninterested in activities that you did in your daily life?"

*ps03-ps04: achieving goals, deal with unexpected events, solve problems
	la def agree 1 "Disagree" 2 "Somewhat disagree" 3 "Somewhat agree" 4 "Agree", replace
	foreach var of varlist ps03 ps04 ps05 {
		tab `var', m
		assert missing(`var') if !missing(`var'_a)
		replace `var' = `var'_a if !missing(`var'_a)
		replace `var' = "1" if `var' == "Tidak setuju"
		replace `var' = "2" if `var' == "Agak setuju"
		replace `var' = "3" if `var' == "Hampir setuju"
		replace `var' = "4" if `var' == "Sangat setuju"
		destring `var', replace
		replace `var' = .a if `var' == -99
		la val `var' agree
		tab `var', m
	}

	rename ps03 accomplish_goals
	la var accomplish_goals "It is easy for me to stick to my aims and accomplish my goals. "

	rename ps04 deal_uncertainty
	la var deal_uncertainty "I am confident that I could deal efficiently with unexpected events. "

	rename ps05 solve_problems
	la var solve_problems "I can solve most problems if I invest the necessary effort."

* ps05: confidence in starting business
	tab ps06, m
	assert missing(ps06) if !missing(ps06_a)
	replace ps06 = ps06_a if !missing(ps06_a)
	replace ps06 = "1" if ps06 == "Sangat tidak percaya diri"
	replace ps06 = "2" if ps06 == "Tidak percaya diri"
	replace ps06 = "3" if ps06 == "Percaya diri"
	replace ps06 = "4" if ps06 == "Sangat percaya diri"
	destring ps06, replace
	replace ps06 = .a if ps06 == -99
	la def confidence 1 "very not confident" 2 "Not confident" 3 "Confident" 4 "very confident", replace
	la val ps06 confidence
	rename ps06 business_ability
	la var business_ability  "How confident do you feel in your ability to start a business?"

	drop ps0*_a

* Normalize
	foreach var in depressed disinterested accomplish_goals deal_uncertainty solve_problems business_ability {
		gen `var'_n = (`var'/4)
	}

* Aggregate
	gen depress_disint = depressed + disinterested
	gen depress_disint_n =depress_disint/8
	gen efficacy = accomplish_goals + deal_uncertainty + solve_problems
	gen efficacy_n = efficacy/12

/*----------------------------------------------------*/
                  /* Section: kp */
/*----------------------------------------------------*/

	rename kp01 apply_kp
	la var apply_kp "Applied for Kartu Prakerja (survey)"
	tab apply_kp, m

	rename kp02 receive_kp
	replace receive_kp = 0 if apply_kp == 0 | apply_kp == .a
	la var receive_kp "Selected for Kartu Prakerja (survey)"
	tab receive_kp, m

	gen receive_kp_date = mdy(kp03_1, 15, kp03_2)
	format receive_kp_date %td
	la var receive_kp_date "What date did you receive Kartu Prakerja?"
	drop kp03*

/************************************************************
	Compensation Payments (section PM)
**************************************************************/

* TRADE-OFF BETWEEN E-WALLET AND PHONE CREDIT VARIABLES

* There are 9 combinations of values * 2 methods of showing showing each combination (phone credit first or ewallet first), so total 18 variables

	foreach var of varlist pm01*p pm01*e {
		replace `var' = "1" if strpos(`var', "E-wallet") > 0
		replace `var' = "1" if strpos(`var', "e-wallet") > 0
		replace `var' = "0" if strpos(`var', "pulsa") > 0
		destring `var', replace
		replace `var' = .a if `var' == -99
		tab `var', m
	}

	forval i = 1/9 {
		replace pm01_`i'_p = pm01_`i'_e if !missing(pm01_`i'_e)
		drop pm01_`i'_e
	}

	la def pm01_1_p 1 "50K e-wallet" 0 "50K phone credit", modify
	la def pm01_2_p 0 "50K phone credit" 1 "55K e-wallet", modify
	la def pm01_3_p 0 "50K phone credit" 1 "60K e-wallet", modify
	la def pm01_4_p 0 "50K phone credit" 1 "65K e-wallet", modify
	la def pm01_5_p 1 "50K e-wallet" 0 "55K phone credit", modify
	la def pm01_6_p 1 "50K e-wallet" 0 "60K phone credit", modify
	la def pm01_7_p 1 "50K e-wallet" 0 "65K phone credit", modify
	la def pm01_8_p 1 "50K e-wallet" 0 "70K phone credit", modify
	la def pm01_9_p 1 "50K e-wallet" 0 "75K phone credit", modify

	la var pm01_1_p "Which would you prefer: 50K phone credit vs. 50K e-wallet?"
	la var pm01_2_p "Which would you prefer: 50K phone credit vs. 55K e-wallet?"
	la var pm01_3_p "Which would you prefer: 50K phone credit vs. 60K e-wallet?"
	la var pm01_4_p "Which would you prefer: 50K phone credit vs. 65K e-wallet?"
	la var pm01_5_p "Which would you prefer: 55K phone credit vs. 50K e-wallet?"
	la var pm01_6_p "Which would you prefer: 60K phone credit vs. 50K e-wallet?"
	la var pm01_7_p "Which would you prefer: 65K phone credit vs. 50K e-wallet?"
	la var pm01_8_p "Which would you prefer: 70k phone credit vs. 50k e-wallet?"
	la var pm01_9_p "Which would you prefer: 75k phone credit vs. 50k e-wallet?"

* rename for ordering from highest phone credit -> lowest phone credit, and lowest e-wallet credit -> highest e-wallet credit
	rename pm01_9_p ewalletvsphone_1 // 50k E-Wallet vs. 75k Phone Credit
	rename pm01_8_p ewalletvsphone_2 // 50k E-Wallet vs. 70k Phone Credit
	rename pm01_7_p ewalletvsphone_3 // 50k E-Wallet vs. 65k Phone Credit
	rename pm01_6_p ewalletvsphone_4 // 50k E-Wallet vs. 60k Phone Creditno
	rename pm01_5_p ewalletvsphone_5 // 50k E-Wallet vs. 55k Phone Credit
	rename pm01_1_p ewalletvsphone_6 // 50k E-Wallet vs. 50k Phone Credit
	rename pm01_2_p ewalletvsphone_7 // 55k E-Wallet vs. 50k Phone Credit
	rename pm01_3_p ewalletvsphone_8 // 60k E-Wallet vs. 50k Phone Credit
	rename pm01_4_p ewalletvsphone_9 // 65k E-Wallet vs. 50k Phone Credit

	tab ewalletvsphone_5

	egen choose_ewallet = rowtotal(ewalletvsphone_*)
	label var choose_ewallet "Choose E-Wallet"


/*----------------------------------------------------*/
                  /* Section: pm */
/*----------------------------------------------------*/

* pm04: whose phone number
	tab pm04, m
	replace pm04 = "1" if pm04 == "Saya/milik pribadi"
	replace pm04 = "2" if pm04 == "Pasangan/suami/istri"
	replace pm04 = "3" if pm04 == "Keluarga/anggota rumah tangga"
	replace pm04 = "4" if pm04 == "Teman"
	replace pm04 = "5" if pm04 == "Lainnya"
	destring pm04, replace
	replace pm04 = .a if pm04 == -99
	la def pm04 1 "Mine" 2 "Spouse's" 3 "Family's/HH Member's" 4 "Friend's" 5 "Other", replace
	la val pm04 pm04
	la var pm04 "Whose phone number is this?"
	tab pm04, m

* pm06: ewallet type
	tab pm06, m
	replace pm06 = "1" if  strpos(pm06,"Go") > 0
	replace pm06 = "2" if  strpos(pm06,"OVO") > 0
	replace pm06 = "3" if  strpos(pm06,"LinkAja") > 0
	replace pm06 = "4" if  strpos(pm06,"Dana") > 0
	destring pm06, replace
	replace pm06 = .a if pm06 == -99

	la def pm06 1 "Gopay" 2 "OVO" 3 "LinkAja" 4 "Dana", replace
	la val pm06 pm06
	la var pm06 "Please select the e-wallet of your choice to receive compensation"
	tab pm06, m

* pm07: whose phone number and e-wallet
	tab pm07, m
	replace pm07 = "1" if pm07 == "Saya/milik pribadi"
	replace pm07 = "2" if pm07 == "Pasangan/suami/istri"
	replace pm07 = "3" if pm07 == "Keluarga/anggota rumah tangga"
	replace pm07 = "4" if pm07 == "Teman"
	replace pm07 = "5" if pm07 == "Lainnya"
	destring pm07, replace
	replace pm07 = .a if pm07 == -99
	la val pm07 pm04
	la var pm07 "Whose phone number and e-wallet is this?"
	tab pm07, m

/*----------------------------------------------------*/
             /* Section: Output Data */
/*----------------------------------------------------*/

* merge with roster
	merge 1:m anon_id4 using "$KP_deid_survey/2022/Raw/Feb5_p22_roster_master.dta", keep(1 2 3 4 5) keepusing(sent_survey_22 control_sample gender test_score education p22_c0 p22_c1 p22_c3 p22_c3) gen(merge_roster) update
	
	replace sent_survey_22 =1 if p22_c3 ==1

* export version un-merged with PMO data
	preserve
	drop if merge_roster==2
		compress
		datasignature 
	    if "`r(datasignature)'" == "19393:528(65675):733591496:1399390568" {
	   	 save "$KP_deid_survey/2022/Clean/071323_survey_22_deid_clean.dta", replace
	   	   }
	 	else {
	     di as err "Careful, your machine produces a different dataset"
	    stop
			}
	restore

	drop if mi(anon_id4)

* merge with admin data
	merge 1:m anon_id4 using "$KP_deid_admin/Clean/pmo_b1-22_clean_long_deid.dta", keep(1 3 4 5) nogen keepusing(gender batch strata stratum_win_prob urban yrs_school multi_apply_hh win_in_batch ever_win_22 ever_win_39 ever_win_47 ever_win_17 education age final_test_score batch_treated_39 batch_treated_47 java date_incentive) update
	compress

* final check of the missing data values
	mdesc

* save control sample only
	tab control_sample, m
	replace control_sample = 0 if missing(control_sample)
	di _N

	preserve
		keep if control_sample == 1
		datasignature 
	    if "`r(datasignature)'" == "46540:545(103347):1493620836:1221527416" {
	   	 save "$KP_deid_survey/2022/Clean/071323_survey_22_deid_clean_control_merged.dta", replace
	   	   }
	 	else {
	     di as err "Careful, your machine produces a different dataset"
	    stop
			}
		
	restore

* Save full sample
datasignature 
	    if "`r(datasignature)'" == "742138:545(103347):2280022660:26994372" {
	   	 save "$KP_deid_survey/2022/Clean/071323_survey_22_deid_clean_merged.dta", replace
	   	   }
	 	else {
	     di as err "Careful, your machine produces a different dataset"
	    stop
			}
	

// DONE
